using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI.HtmlControls;

namespace MessageBoardControl
{
	/// <summary>
	/// Summary description for AddThread.
	/// </summary>
	[DefaultProperty("Text"), 
		ToolboxData("<{0}:AddThread runat=server></{0}:AddThread>")]
	public class AddThread : System.Web.UI.WebControls.WebControl, INamingContainer
	{
		#region Properties
		private string text;
	
		[Bindable(true), 
			Category("Appearance"), 
			DefaultValue("")] 
		public string Text 
		{
			get
			{
				return text;
			}

			set
			{
				text = value;
			}
		}

		private string urlAfter;
		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string URLAfter 
		{
			get
			{
				return urlAfter;
			}

			set
			{
				urlAfter = value;
			}
		}

		private MessageBoard.Forum forum;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.Forum Forum 
		{
			get
			{
				return forum;
			}

			set
			{
				forum = value;
			}
		}

		private string imagesFolder;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string ImagesFolder 
		{
			get
			{
				return imagesFolder;
			}

			set
			{
				imagesFolder = value;
			}
		}

		private MessageBoard.User currentUser;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.User CurrentUser 
		{
			get
			{
				return currentUser;
			}

			set
			{
				currentUser = value;
			}
		}

		private MessageBoard.Language language;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.Language Language 
		{
			get
			{
				return language;
			}

			set
			{
				language = value;
			}
		}
		#endregion

		/// <summary> 
		/// Render this control to the output parameter specified.
		/// </summary>
		/// <param name="output"> The HTML writer to write out to </param>
		//protected override void Render(HtmlTextWriter output)
		//{
		//	output.Write(Text);
		//}

		protected override void CreateChildControls() 
		{
			// Check the required permissions
			if(MessageBoard.Role.Check(currentUser, Forum, MessageBoard.Role.Permission.CanAddThreads) !=  MessageBoard.Role.Allow.Yes)
			{
				return;
			}

			// Save the last request time by the user.
			CurrentUser.MakeRequest();

			// Define the Objects
			TextBox txtSubject = new TextBox();	// The subject text box.
			TextBox txtText = new TextBox();	// The body of the post.
			Button cmdAdd = new Button();	// The command button.
			
			// Define the subject text box.
			txtSubject.Text = "";
			txtSubject.MaxLength = 255;
			txtSubject.Width = 500;
			txtSubject.ID="txtSubject";

			// Define the body text box.
			txtText.Text="";
			txtText.TextMode = TextBoxMode.MultiLine;
			txtText.Height = 200;
			txtText.Width = 500;
			txtText.ID = "txtText";

			// Define the post button.
			cmdAdd.Text = Translate("Post");

			// Put the contols in a table.
			HtmlTable table = new HtmlTable();
			table.Attributes.Add("width", "100%");
			HtmlTableRow tr0 = new HtmlTableRow();
			table.Controls.Add(tr0);
			HtmlTableCell td0 = new HtmlTableCell();
			tr0.Controls.Add(td0);
			td0.Attributes.Add("class", "Forum_Title1");
			td0.InnerHtml = Translate("Add new Thread");

			HtmlTableRow tr01 = new HtmlTableRow();
			table.Controls.Add(tr01);
			HtmlTableCell td01 = new HtmlTableCell();
			tr01.Controls.Add(td01);
			td01.Attributes.Add("class", "Forum_Title4");

			// Table 2
			HtmlTable table2 = new HtmlTable();
			table2.Attributes.Add("width", "100%");



			// Subject
			HtmlTableRow tr1 = new HtmlTableRow();
			table2.Controls.Add(tr1);
			HtmlTableCell td11 = new HtmlTableCell();
			HtmlTableCell td12 = new HtmlTableCell();
			td11.InnerHtml = Translate("Subject")+":";
			td11.Attributes.Add("class", "Forum_Text");
			td12.Controls.Add(txtSubject);
			tr1.Controls.Add(td11);
			tr1.Controls.Add(td12);
			

			// Icons
			RadioButton rdio1 = new RadioButton();
			RadioButton rdio2 = new RadioButton();
			RadioButton rdio3 = new RadioButton();
			RadioButton rdio4 = new RadioButton();
			RadioButton rdio5 = new RadioButton();
			/*
			RadioButton rdio6 = new RadioButton();
			RadioButton rdio7 = new RadioButton();
			RadioButton rdio8 = new RadioButton();
			RadioButton rdio9 = new RadioButton();
			RadioButton rdio10 = new RadioButton();
			RadioButton rdio11 = new RadioButton();
			RadioButton rdio12 = new RadioButton();
			RadioButton rdio13 = new RadioButton();
			RadioButton rdio14 = new RadioButton();
			*/
			rdio1.GroupName = "icons";
			rdio2.GroupName = "icons";
			rdio3.GroupName = "icons";
			rdio4.GroupName = "icons";
			rdio5.GroupName = "icons";
			/*
			rdio6.GroupName = "icons";
			rdio7.GroupName = "icons";
			rdio8.GroupName = "icons";
			rdio9.GroupName = "icons";
			rdio10.GroupName = "icons";
			rdio11.GroupName = "icons";
			rdio12.GroupName = "icons";
			rdio13.GroupName = "icons";
			rdio14.GroupName = "icons";
			*/
			rdio1.Checked = true;

			rdio1.ID = "rdio1";
			rdio2.ID = "rdio2";
			rdio3.ID = "rdio3";
			rdio4.ID = "rdio4";
			rdio5.ID = "rdio5";
			/*
			rdio6.ID = "rdio6";
			rdio7.ID = "rdio7";
			rdio8.ID = "rdio8";
			rdio9.ID = "rdio9";
			rdio10.ID = "rdio10";
			rdio11.ID = "rdio11";
			rdio12.ID = "rdio12";
			rdio13.ID = "rdio13";
			rdio14.ID = "rdio14";	
			*/		

			rdio1.GroupName = "icons";
			Image img1 = new Image();
			Image img2 = new Image();
			Image img3 = new Image();
			Image img4 = new Image();
			Image img5 = new Image();
			/*
			Image img6 = new Image();
			Image img7 = new Image();
			Image img8 = new Image();
			Image img9 = new Image();
			Image img10 = new Image();
			Image img11 = new Image();
			Image img12 = new Image();
			Image img13 = new Image();
			Image img14 = new Image();
			*/
			img1.ImageUrl = imagesFolder+"/icon1.gif";
			img2.ImageUrl = imagesFolder+"/icon2.gif";
			img3.ImageUrl = imagesFolder+"/icon3.gif";
			img4.ImageUrl = imagesFolder+"/icon4.gif";
			img5.ImageUrl = imagesFolder+"/icon5.gif";
			/*
			img6.ImageUrl = imagesFolder+"/icon6.gif";
			img7.ImageUrl = imagesFolder+"/icon7.gif";
			img8.ImageUrl = imagesFolder+"/icon8.gif";
			img9.ImageUrl = imagesFolder+"/icon9.gif";
			img10.ImageUrl = imagesFolder+"/icon10.gif";
			img11.ImageUrl = imagesFolder+"/icon11.gif";
			img12.ImageUrl = imagesFolder+"/icon12.gif";
			img13.ImageUrl = imagesFolder+"/icon13.gif";
			img14.ImageUrl = imagesFolder+"/icon14.gif";
			*/
			
			HtmlTableRow tr2 = new HtmlTableRow();
			table2.Controls.Add(tr2);
			HtmlTableCell td21 = new HtmlTableCell();
			HtmlTableCell td22 = new HtmlTableCell();
			td21.InnerHtml = Translate("Select Icon")+":";
			td21.Attributes.Add("class", "Forum_Text");
			td21.Attributes.Add("vAlign", "top");
			td22.Controls.Add(rdio1);
			td22.Controls.Add(img1);
			td22.Controls.Add(rdio2);
			td22.Controls.Add(img2);
			td22.Controls.Add(rdio3);
			td22.Controls.Add(img3);
			td22.Controls.Add(rdio4);
			td22.Controls.Add(img4);
			td22.Controls.Add(rdio5);
			td22.Controls.Add(img5);
			/*
			td22.Controls.Add(rdio6);
			td22.Controls.Add(img6);
			td22.Controls.Add(rdio7);
			td22.Controls.Add(img7);
			td22.Controls.Add(rdio8);
			td22.Controls.Add(img8);
			td22.Controls.Add(rdio9);
			td22.Controls.Add(img9);
			td22.Controls.Add(rdio10);
			td22.Controls.Add(img10);
			td22.Controls.Add(rdio11);
			td22.Controls.Add(img11);
			td22.Controls.Add(rdio12);
			td22.Controls.Add(img12);
			td22.Controls.Add(rdio13);
			td22.Controls.Add(img13);
			td22.Controls.Add(rdio14);
			td22.Controls.Add(img14);
			*/

			tr2.Controls.Add(td21);
			tr2.Controls.Add(td22);


			// Text body
			HtmlTableRow tr3 = new HtmlTableRow();
			table2.Controls.Add(tr3);
			HtmlTableCell td31 = new HtmlTableCell();
			HtmlTableCell td32 = new HtmlTableCell();
			td31.InnerHtml = Translate("Message")+":";
			td31.Attributes.Add("class", "Forum_Text");
			td31.Attributes.Add("vAlign", "top");
			td32.Controls.Add(txtText);
			tr3.Controls.Add(td31);
			tr3.Controls.Add(td32);

			// Button
			HtmlTableRow tr4 = new HtmlTableRow();
			table2.Controls.Add(tr4);
			HtmlTableCell td41 = new HtmlTableCell();
			HtmlTableCell td42 = new HtmlTableCell();
			td41.InnerHtml = "";
			td42.Controls.Add(cmdAdd);
			tr4.Controls.Add(td41);
			tr4.Controls.Add(td42);

			td01.Controls.Add(table2);

			HtmlTable tableOut = new HtmlTable();
			HtmlTableRow trOut = new HtmlTableRow();
			HtmlTableCell tdOut = new HtmlTableCell();
			tableOut.BgColor = "#000000";
			tableOut.Attributes.Add("width", "100%");
			tableOut.CellPadding = 0;
			tableOut.CellSpacing = 0;
			table.CellSpacing = 1;
			tdOut.Controls.Add(table);
			trOut.Controls.Add(tdOut);
			tableOut.Controls.Add(trOut);
			Controls.Add(tableOut);
			

			// The Button event.
			cmdAdd.Click += new EventHandler(cmdAdd_Click);
                  
		}

		public void cmdAdd_Click(Object sender, EventArgs e)
		{
			TextBox txtSubject = ((TextBox)FindControl("txtSubject"));
			TextBox txtText = ((TextBox)FindControl("txtText"));
			RadioButton rdio1 = ((RadioButton)FindControl("rdio1"));
			RadioButton rdio2 = ((RadioButton)FindControl("rdio2"));
			RadioButton rdio3 = ((RadioButton)FindControl("rdio3"));
			RadioButton rdio4 = ((RadioButton)FindControl("rdio4"));
			RadioButton rdio5 = ((RadioButton)FindControl("rdio5"));
			/*
			RadioButton rdio6 = ((RadioButton)FindControl("rdio6"));
			RadioButton rdio7 = ((RadioButton)FindControl("rdio7"));
			RadioButton rdio8 = ((RadioButton)FindControl("rdio8"));
			RadioButton rdio9 = ((RadioButton)FindControl("rdio9"));
			RadioButton rdio10 = ((RadioButton)FindControl("rdio10"));
			RadioButton rdio11 = ((RadioButton)FindControl("rdio11"));
			RadioButton rdio12 = ((RadioButton)FindControl("rdio12"));
			RadioButton rdio13 = ((RadioButton)FindControl("rdio13"));
			RadioButton rdio14 = ((RadioButton)FindControl("rdio14"));
			*/
			if(txtSubject.Text!="" || txtText.Text!="")
			{
				// Add a new thread.
				MessageBoard.Thread thread = new MessageBoard.Thread();
				thread.Subject= txtSubject.Text;				
				thread.TotalPosts = 1;
				thread.TotalViews = 1;
				thread.Locked = false;
				thread.Forum = Forum;
				if(rdio1.Checked)
					thread.Icon = 1;
				else if(rdio2.Checked)
					thread.Icon = 2;
				else if(rdio3.Checked)
					thread.Icon = 3;
				else if(rdio4.Checked)
					thread.Icon = 4;
				else if(rdio5.Checked)
					thread.Icon = 5;
					/*
				else if(rdio6.Checked)
					thread.Icon = 6;
				else if(rdio7.Checked)
					thread.Icon = 7;
				else if(rdio8.Checked)
					thread.Icon = 8;
				if(rdio9.Checked)
					thread.Icon = 9;
				else if(rdio10.Checked)
					thread.Icon = 10;
				else if(rdio11.Checked)
					thread.Icon = 11;
				else if(rdio12.Checked)
					thread.Icon = 12;
				else if(rdio13.Checked)
					thread.Icon = 13;
				else if(rdio14.Checked)
					thread.Icon = 14;
				*/
				thread.Insert();
				
				// Add the first post of the thread.
				MessageBoard.Post post = new MessageBoard.Post();
				post.Text = txtText.Text;
				post.Thread = thread;
				post.User = CurrentUser;
				post.IsFirstPostInThread = true;
				post.Insert();

				// Refresh.
				txtSubject.Text="";
				txtText.Text="";
				this.EnsureChildControls();
				this.Page.Response.Redirect(urlAfter);
			}
		}

		private MessageBoard.CustomControl customControl = MessageBoard.CustomControl.Get("AddThread");

		private string Translate(string word)
		{
			return MessageBoard.Translation.Get(customControl, word, Language).Equal;
		}
	}
}
